package com.ixuestar.controller;

import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.ixuestar.service.IxuestarParentsService;
import com.ixuestar.util.ReturnResult;
import com.ixuestar.wxutil.GetwxOpenId;
import com.ixuestar.wxutil.PayConfigUtil;

@CrossOrigin
@RestController
@RequestMapping(value = "/ixuestarParents",method = RequestMethod.POST)
public class IxuestarParentsController extends BaseController{

	//日志打印
	public Logger logger = Logger.getLogger(IxuestarParentsController.class);
	
	//创建接口对
	@Autowired
	public IxuestarParentsService ixuestarParentsService;
	
	/**
	 * @method 通过手机号获取老师信息
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@RequestMapping("/getWxSessionKey")
	@ResponseBody
	public Map<String, Object> getWxSessionKey(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		//创建返回参数
		Map<String, Object> map = null;
		try {
			String appId = PayConfigUtil.ixuestarwx_APP_ID_J;
			String appSec = PayConfigUtil.ixuestarwx_APP_SEC_J;
			logger.info("-----------进入getWxSessionKey接口-------------");
			String code = jsonObject.getString("code");
			//执行接口
			JSONObject wxSessionKey = GetwxOpenId.getWxSessionKey(code,appId,appSec);
//			jsonObject.put("wxSessionKey", wxSessionKey);
//			jsonObject.remove("code");
			//
			map = ReturnResult.correctResult(wxSessionKey);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			map = ReturnResult.wrongResult();
		}
		return map;
	}
	
	/**
	 * @method 通过手机号获取老师信息
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@RequestMapping("/getWxTelephone")
	@ResponseBody
	public Map<String, Object> getWxTelephone(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		//创建返回参数
		Map<String, Object> map = null;
		try {
			logger.info("-----------进入getWxTelephone接口-------------");
			String wxIv = jsonObject.getString("wxIv");
			String wxSessionKey = jsonObject.getString("wxSessionKey");
			String encryptedData = jsonObject.getString("encryptedData");
			//执行接口
			final JSONObject wxTelephone = GetwxOpenId.getWxTelephone(encryptedData, wxSessionKey, wxIv);
			logger.info("--------wxTelephone-"+wxTelephone+"----------");
			map = ReturnResult.correctResult(wxTelephone);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			map = ReturnResult.wrongResult();
		}
		return map;
	}
	
	/**
	 * @method 手机号绑定会员信息 
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/bindingInfoByTelephone")
	@ResponseBody
	public Map<String, Object> bindingInfoByTelephone(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		Map<String, Object> map = null;
		//判断参数对象是否为空
		this.validateObjectNullException(jsonObject, "对象中参数不能为空");
		try {
			//调用绑定接口
			JSONObject bindingInfoByTelephone = ixuestarParentsService.bindingInfoByTelephone(jsonObject);
			Integer code = bindingInfoByTelephone.getInteger("code");
			if (code==0) {
				bindingInfoByTelephone.remove("code");
				map = ReturnResult.correctResult(bindingInfoByTelephone);
			}else if(code==1){
				map = ReturnResult.otherResult(1, "会员卡号错误");
			}else if (code==2) {
				map = ReturnResult.otherResult(2, "手机号已绑定学生会员号");
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			map = ReturnResult.wrongResult();
		}
		return map;
	}
	
	/**
	 * @method 通过输入的卡号获取绑定的学生会员信息
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getStudentInfoByIxmNo")
	@ResponseBody
	public Map<String, Object> getStudentInfoByIxmNo(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		Map<String, Object> map = null;
		//判断参数对象是否为空
		this.validateObjectNullException(jsonObject, "对象中参数不能为空");
		try {
			//调用绑定接口
			JSONObject studentInfoByIxmNo = ixuestarParentsService.getStudentInfoByIxmNo(jsonObject);
			Integer code = studentInfoByIxmNo.getInteger("code");
			if (code==0) {
				studentInfoByIxmNo.remove("code");
				map = ReturnResult.correctResult(studentInfoByIxmNo);
			}else {
				map = ReturnResult.otherResult(1, "会员卡号错误");
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			map = ReturnResult.wrongResult();
		}
		return map;
	}
	
	/**
	 * @method 获取学校列表
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getSchool")
	@ResponseBody
	public Map<String, Object> getSchool(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		//非空判断
		this.validateObjectNullException(jsonObject, "对象中参数不能为空");
		Map<String, Object> map = null;
		try {
			JSONObject schoolList = ixuestarParentsService.getSchoolInfos();
			//返回正确结果
			map = ReturnResult.correctResult(schoolList);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			map = ReturnResult.wrongResult();
		}
		return map;
	}
	
	/**
	 * @method获取学校年级
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@RequestMapping("/getSchoolGrade")
	@ResponseBody
	public Map<String, Object> getSchoolGrade(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		//非空判断
		this.validateObjectNullException(jsonObject, "对象中参数不能为空");
		Map<String, Object> map = null;
		try {
			JSONObject schoolGradeList = ixuestarParentsService.getSchoolGradeList(jsonObject);
			//正确返回
			map = ReturnResult.correctResult(schoolGradeList);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			map = ReturnResult.wrongResult();
		}
		return map;
	}
	
	/**
	 * @method获取年级下的班级
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@RequestMapping("/getSchoolClass")
	@ResponseBody
	public Map<String, Object> getSchoolClass(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		//非空判断
		this.validateObjectNullException(jsonObject, "对象中参数不能为空");
		Map<String, Object> map = null;
		try {
			JSONObject schoolClassList = ixuestarParentsService.getSchoolClassList(jsonObject);
			//正确返回
			map = ReturnResult.correctResult(schoolClassList);
		} catch (Exception e) { 
			// TODO: handle exception
			e.printStackTrace();
			map = ReturnResult.wrongResult();
		}
		return map;
	}
	
	/**
	 * @method 获取班级的学生信息
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@RequestMapping("/getSchoolStudent")
	@ResponseBody
	public Map<String, Object> getSchoolStudent(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		//非空判断
		this.validateObjectNullException(jsonObject, "对象中参数不能为空");
		Map<String, Object> map = null;
		try {
			JSONObject schoolStudentList = ixuestarParentsService.getSchoolStudentList(jsonObject);
			//正确返回
			map = ReturnResult.correctResult(schoolStudentList);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			map = ReturnResult.wrongResult();
		}
		return map;
	} 
	
	/**
	 * @method 获取绑定的学生信息
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getBindingInfo")
	@ResponseBody
	public Map<String, Object> getBindingInfo(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		//非空判断
		this.validateObjectNullException(jsonObject, "对象中参数不能为空");
		Map<String, Object> map = null;
		try {
			JSONObject schoolStudentInfo = ixuestarParentsService.getSchoolStudentInfo(jsonObject);
			Integer code = schoolStudentInfo.getInteger("code");
			if (code==0) {
				//正确返回
				map = ReturnResult.correctResult(schoolStudentInfo);				
			}else {
				map = ReturnResult.otherResult(code, "该手机号绑定学生信息，请绑定后重新登录");
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			map = ReturnResult.wrongResult();
		}
		return map;
	}
	
	/**
	 * @method 获取考试列表
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getExaminationList")
	@ResponseBody
	public Map<String, Object> getExaminationList(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		Map<String, Object> map = null;
		try {
			//调用接口
			JSONObject examinationList = ixuestarParentsService.getExaminationList(jsonObject);
			//获取code
			Integer code = examinationList.getInteger("code");
			if (code==0) {
				//返回map
				map = ReturnResult.correctResult(examinationList);
			}else {
				map = ReturnResult.otherResult(code, "没有考试列表，可能该年级还没有进行考试");
			}
		} catch (Exception e) {
			// TODO: handle exception
			map = ReturnResult.wrongResult();
		}
		return map;
	}
	
	/**
	 * @Method 获取小程序首页信息
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getExaminationResult")
	@ResponseBody
	public Map<String, Object>getExaminationResult(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		Map<String, Object> map = null;
		try {
			JSONObject examinationResultSubject = ixuestarParentsService.getExaminationResultSubject(jsonObject);
			//正确返回
			map = ReturnResult.correctResult(examinationResultSubject);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			map = ReturnResult.wrongResult();
		}
		return map;
	}
	
	/**
	 * @method 获取最近两次的考试成绩
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getExaminationResultCompare")
	@ResponseBody
	public Map<String, Object>getExaminationResultCompare(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		Map<String, Object> map = null;
		try {
			JSONObject examinationResultCompare = ixuestarParentsService.getExaminationResultCompare(jsonObject);
			Integer integer = examinationResultCompare.getInteger("code");
			if (integer==0) {
				examinationResultCompare.remove("code");
				//正确返回
				map = ReturnResult.correctResult(examinationResultCompare);				
			}else if (integer==1) {
				map = ReturnResult.otherResult(1, "本次考试中未查找到您的成绩，可能您未参加考试");
			}else if (integer==2) {
				map = ReturnResult.otherResult(1, "上次考试中未查找到您的成绩，可能您未参加考试");
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			map = ReturnResult.wrongResult();
		}
		return map;
	}
	
	/**
	 * @method 获取科目的详细信息
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getExaminationSubjectDetail")
	@ResponseBody
	public Map<String, Object>getExaminationSubjectDetail(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		Map<String, Object> map = null;
		try {
			JSONObject examinationSubjectDetail = ixuestarParentsService.getExaminationSubjectDetail(jsonObject);
			//正确返回
			map = ReturnResult.correctResult(examinationSubjectDetail);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			map = ReturnResult.wrongResult();
		}
		return map;
	}
	
	/**
	 * @method 获取科目的题目的得分信息
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getExaminationQuestionInfo")
	@ResponseBody
	public Map<String, Object>getExaminationQuestionInfo(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		Map<String, Object> map = null;
		try {
			JSONObject examinationQuestionInfo = ixuestarParentsService.getExaminationQuestionInfo(jsonObject);
			//正确返回
			map = ReturnResult.correctResult(examinationQuestionInfo);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			map = ReturnResult.wrongResult();
		}
		return map;
	}
}
